package com.cykj.order.controller;

import com.cykj.common.core.web.controller.BaseController;
import com.cykj.common.core.web.domain.AjaxResult;
import com.cykj.common.core.web.page.TableDataInfo;
import com.cykj.order.domain.BsOrders;
import com.cykj.order.domain.dto.OrderProductDetailDto;
import com.cykj.order.domain.vo.BsOrdersVo;
import com.cykj.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单管理Controller
 * 
 * @author cykj
 * @date 2025-08-18
 */
@RestController
public class OrderController extends BaseController {
    
    @Autowired
    private IOrderService orderService;
    
    /**
     * 查询订单列表（带分页）
     */
    @GetMapping("/list")
    public TableDataInfo list(BsOrders bsOrders) {
        startPage();
        List<BsOrdersVo> list = orderService.selectOrderList(bsOrders);
        return getDataTable(list);
    }
    
    /**
     * 查询订单列表（不分页）
     */
    @GetMapping("/listAll")
    public AjaxResult listAll(BsOrders bsOrders) {
        List<BsOrdersVo> list = orderService.selectOrderList(bsOrders);
        return success(list);
    }
    
    /**
     * 根据订单ID获取订单详细信息（包含用户、商家等扩展信息）
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        BsOrdersVo orderVo = orderService.selectOrderById(id);
        return success(orderVo);
    }
    
    /**
     * 根据订单号获取订单详细信息
     */
    @GetMapping(value = "/orderNumber/{orderNumber}")
    public AjaxResult getInfoByOrderNumber(@PathVariable("orderNumber") String orderNumber) {
        BsOrdersVo orderVo = orderService.selectOrderByOrderNumber(orderNumber);
        return success(orderVo);
    }
    
    /**
     * 根据订单ID查询商品详情（包含商品信息和订单明细）
     */
    @GetMapping(value = "/{orderId}/product")
    public AjaxResult getOrderProductDetails(@PathVariable("orderId") Long orderId) {
        List<OrderProductDetailDto> productDetails = orderService.selectOrderProductDetailsByOrderId(orderId);
        return success(productDetails);
    }
    
    /**
     * 修改订单
     */
    @PutMapping
    public AjaxResult edit(@RequestBody BsOrders bsOrders) {
        return toAjax(orderService.updateOrder(bsOrders));
    }
}
